<template>
  <div class="register-wrapper">
    <el-card class="register-card">
      <template #header>
        <div class="card-header">
          <span>用户注册</span>
        </div>
      </template>

      <el-form
        ref="registerFormRef"
        :model="registerForm"
        :rules="rules"
        label-width="80px"
        @keyup.enter="handleRegister"
      >
        <!-- 用户名 -->
        <el-form-item label="用户名" prop="userName">
          <el-input
            v-model="registerForm.userName"
            placeholder="请输入用户名"
            clearable
          >
            <template #prefix><User /></template>
          </el-input>
        </el-form-item>

        <!-- 密码 -->
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="registerForm.password"
            type="password"
            placeholder="请输入密码"
            show-password
            clearable
          >
            <template #prefix><Lock /></template>
          </el-input>
        </el-form-item>

        <!-- 确认密码 -->
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input
            v-model="registerForm.confirmPassword"
            type="password"
            placeholder="请再次输入密码"
            show-password
            clearable
          >
            <template #prefix><Lock /></template>
          </el-input>
        </el-form-item>
        <!-- 注册按钮 -->
        <el-form-item>
          <el-button
            type="primary"
            style="width: 100%"
            :loading="loading"
            @click="handleRegister"
          >
            注册
          </el-button>
        </el-form-item>

        <!-- 返回登录 -->
        <el-form-item>
          <div class="register-footer">
            已有账号？<router-link to="/login" class="login-link">立即登录</router-link>
          </div>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { User, Lock, Message, Phone } from '@element-plus/icons-vue'
import { userRegister } from '../request/login'
import router from '../router';

// 表单引用
const registerFormRef = ref()

// 表单数据
const registerForm = reactive({
  userName: '',
  password: '',
  confirmPassword: '',
  email: '',
  phoneNo: ''
})

// 校验规则
const rules = {
  userName: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 16, message: '长度在 3 到 16 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' },
    { 
      pattern: /^(?=.*[a-zA-Z])(?=.*\d)[a-zA-Z\d._@#$%^&*!-]{6,}$/, 
      message: '密码必须包含字母和数字', 
      trigger: 'blur' 
    }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    { 
      validator: (rule, value, callback) => {
        if (value !== registerForm.password) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ],
}

// 注册状态
const loading = ref(false)

// 注册方法
const handleRegister = () => {
  registerFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      // 模拟注册请求
      try {
        // 尝试真实的API调用
        const res = await userRegister(registerForm)
        ElMessage.success('注册成功！')
        loading.value = false
        // 注册成功后跳转到登录页面
        router.push('/login')
      } catch (err) {
        loading.value = false
        ElMessage.error(err.message || '注册失败，请重试')
      }
    }
  })
}
</script>

<style scoped>
.register-wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: linear-gradient(135deg, #83a4d4, #b6fbff);
}

.register-card {
  width: 400px;
  padding: 20px;
}

.card-header {
  font-size: 18px;
  font-weight: bold;
  text-align: center;
}

.register-footer {
  text-align: center;
  margin-top: 10px;
}

.login-link {
  color: #1890ff;
  text-decoration: none;
}

.login-link:hover {
  text-decoration: underline;
}
</style>